<!Doctype html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<title>7.2 Verilog 文件操作 | 菜鸟教程</title>

  <meta name='robots' content='max-image-preview:large' />
<link rel='stylesheet' id='classic-theme-styles-css' href='../0/classic-themes.min.css?ver=6.2' type='text/css' media='all' />
<link rel="canonical" href="../w3cnote/verilog2-file.html" />
<meta name="keywords" content="7.2 Verilog 文件操作">
<meta name="description" content="Verilog 提供了很多可以对文件进行操作的系统任务。经常使用的系统任务主要包括：  文件开、闭：$fopen, $fclose, $ferror 文件写入：$fdisplay, $fwrite, $fstrobe, $fmonitor 字符串写入：$sformat, $swrite 文件读取：$fgetc, $fgets, $fscanf, $fread 文件定位：$fseek, $ftell, $feof, $frewind 存储..">
		
	<link rel="shortcut icon" href="https://static.runoob.com/images/favicon.ico">
	<link rel="stylesheet" href="../0/style.css?v=1.170" type="text/css" media="all" />	
	<link rel="stylesheet" href="../0/font-awesome.min.css" media="all" />	
  <!--[if gte IE 9]><!-->
  <script src=""></script>
  <!--<![endif]-->
  <!--[if lt IE 9]>
     <script src=""></script>
     <script src=""></script>
  <![endif]-->
  <link rel="apple-touch-icon" href="https://static.runoob.com/images/icon/mobile-icon.png"/>
  <meta name="apple-mobile-web-app-title" content="菜鸟教程">
</head>
<body>

<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" placeholder="搜索……" name="s" autocomplete="off">
      
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="../">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        
    <div class="col search search-desktop last">
      <div class="search-input" >
      <form action="//www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" placeholder="搜索……"  autocomplete="off" style="height: 44px;">
      </form>
      
      </div>
    </div>
  </div>
</div>



<!-- 导航栏 -->
<div class="container navigation">
    <div class="row">
        <div class="col nav">
            

                        <ul class="pc-nav" id="note-nav">
                <li><a href="../">首页</a></li>
                <li><a href="../w3cnote">笔记首页</a></li>
                <li><a href="../w3cnote/android-tutorial-intro.html" title="Android 基础入门教程">Android</a></li>
                <li><a href="../w3cnote/es6-tutorial.html" title="ES6 教程">ES6 教程</a></li>
                <li><a href="../w3cnote/ten-sorting-algorithm.html" title="排序算法">排序算法</a></li>
                <li><a href="../w3cnote/hadoop-tutorial.html" title="Hadoop 教程">Hadoop</a></li>
                <li><a href="../w3cnote/zookeeper-tutorial.html" title="Zookeeper 教程">Zookeeper</a></li>
                <li><a href="../w3cnote/verilog-tutorial.html" title="Verilog 教程">Verilog</a></li>
                <li><a href="../w3cnote_genre/code" title="编程技术">编程技术</a></li> 
                <li><a href="../w3cnote_genre/coderlife" title="程序员人生">程序员人生</a></li>
                
                <!--<li><a href="javascript:;" class="runoob-pop">登录</a></li>
                
                
                        <li>
                <a style="font-weight:bold;" href="../linux/linux-tutorial.html#yunserver" target="_blank" onclick="_hmt.push(['_trackEvent', 'aliyun', 'click', 'aliyun'])" title="kkb">云服务器</a>
                </li>
                <li><a href="http://gk.link/a/104mQ" target="_blank" style="font-weight: bold;"onclick="_hmt.push(['_trackEvent', '极客时间', 'click', 'jike'])" title="我的圈子">极客时间</a></li>
            
                
                <li><a target="_blank" href="../shoppinglist" rel="nofollow">知识店铺</a></li> 
        -->
            </ul>
                        
              
            <ul class="mobile-nav">
                <li><a href="../w3cnote">首页</a></li>
                <li><a href="../w3cnote_genre/android" target="_blank" title="Android 基础入门教程">Android</a></li>
                <li><a href="../w3cnote/es6-tutorial.html" target="_blank" title="ES6 教程">ES6</a></li>
                <li><a href="../w3cnote_genre/joke" target="_blank" title="程序员笑话">逗乐</a></li>
                
                <a href="javascript:void(0)" class="search-reveal">Search</a> 
            </ul>
            
        </div>
    </div>
</div>


<!--  内容  -->
<div class="container main">
	<div class="row">

		<!--  Android 基础入门教程 start  -->
	<div class="col left-column" style="display:none;">
		<div class="tab">Verilog 高级教程</div>
		<div class="sidebar-box gallery-list">
			<div class="design" id="leftcolumn">  
			</div> 
		</div> 
	</div>
	<!--  Android 基础入门教程 end  -->
		<div class="col middle-column big-middle-column">
	 			<div class="article">
			<div class="article-heading">
				<h2>7.2 Verilog 文件操作</h2>				<h3><em>分类</em> <a href="../w3cnote_genre/verilog2" title="Verilog 教程高级篇" >Verilog 教程高级篇</a> </h3>
			</div>
			<div class="article-body note-body">
				<div class="article-intro">
					<p>Verilog 提供了很多可以对文件进行操作的系统任务。经常使用的系统任务主要包括：</p>
<ul><li>
文件开、闭：$fopen, $fclose, $ferror
</li><li>文件写入：$fdisplay, $fwrite, $fstrobe, $fmonitor
</li><li>字符串写入：$sformat, $swrite
</li><li>文件读取：$fgetc, $fgets, $fscanf, $fread
</li><li>文件定位：$fseek, $ftell, $feof, $frewind
</li><li>存储器加载：$readmemh, $readmemb
</li></ul><p>
使用文件操作任务（尤其注意 $sforamt, $gets, $sscanf 等）对文件进行操作时，需要根据文件性质和变量内容确定使用哪一种系统任务，并保证参数及读写变量类型与文件内容的一致性，不要将字符串类型和多进制类型相混淆。</p>
<h3>文件打开/关闭</h3>
<table class="reference"><thead><tr><th style="text-align:left;"><span>系统任务</span></th><th style="text-align:left;"><span>调用格式</span></th><th style="text-align:left;"><span>任务描述</span></th></tr></thead><tbody><tr><td style="text-align:left;"><span>文件打开</span></td><td style="text-align:left;"><span>fd = $fopen("fname", mode) ;</span></td><td style="text-align:left;"><span>fname 为打开文件的名字</span><br><span>fd 为返回的 32bit 文件描述符</span><br><span>--- 正确打开时，fd 为非零值</span><br><span>--- 打开出错时，fd为零值</span><br><span>mode 用于指定文件打开的方式</span></td></tr><tr><td style="text-align:left;"><span>文件关闭</span></td><td style="text-align:left;"><span>$fclose(fd) ;</span></td><td style="text-align:left;"><span>关闭 fd 描述的对应文件</span></td></tr><tr><td style="text-align:left;"><span>文件错误</span></td><td style="text-align:left;"><span>err = $ferror(fd, str) ;</span></td><td style="text-align:left;"><span>正常打开文件时：</span><br><span>--- err 与 str 均为零值，</span><br><span>打开文件出错时：</span><br><span>--- err 返回非零值表示错误</span><br><span>--- str 返回非零值存储错误类型</span><br><span>--- 官方建议 str 长度为 640bit 位宽</span></td></tr></tbody></table>

<p>举例代码如下：</p>

<div class="example"><h2 class="example">实例</h2> <div class="example_code">
&nbsp; <span style="color: #00008B; font-style: italic;">//open/close file</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">integer</span> fd1<span style="color: #5D478B;">,</span> fd2 <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">integer</span> err1<span style="color: #5D478B;">,</span> err2 <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">reg</span> <span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">320</span><span style="color: #5D478B;">:</span><span style="color: #ff0055;">0</span><span style="color: #9F79EE;">&#93;</span> str1<span style="color: #5D478B;">,</span> str2 <span style="color: #5D478B;">;</span> <span style="color: #00008B; font-style: italic;">//错误类型的变量也可以为可支持的 string 类型</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">initial</span> <span style="color: #A52A2A; font-weight: bold;">begin</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #00008B; font-style: italic;">//existing file</span><br />
&nbsp; &nbsp; &nbsp; fd1 <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$fopen</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;./DATA_RD.HEX&quot;</span><span style="color: #5D478B;">,</span> <span style="color: #FF00FF;">&quot;r&quot;</span><span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span> &nbsp; &nbsp;<span style="color: #00008B; font-style: italic;">//打开存在的文件</span><br />
&nbsp; &nbsp; &nbsp; err1 <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$ferror</span><span style="color: #9F79EE;">&#40;</span>fd1<span style="color: #5D478B;">,</span> str1<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;File1 descriptor is: %h.&quot;</span><span style="color: #5D478B;">,</span> fd1 <span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><span style="color: #00008B; font-style: italic;">//非零值</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;Error1 number is: %h.&quot;</span><span style="color: #5D478B;">,</span> err1 <span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span> &nbsp;<span style="color: #00008B; font-style: italic;">//0</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;Error2 info is: %s.&quot;</span><span style="color: #5D478B;">,</span> str1 <span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span> &nbsp; &nbsp;<span style="color: #00008B; font-style: italic;">//0</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$fclose</span><span style="color: #9F79EE;">&#40;</span>fd1<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #00008B; font-style: italic;">//not existing file</span><br />
&nbsp; &nbsp; &nbsp; fd2 <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$fopen</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;../../FILE_NOEXIST.HEX&quot;</span><span style="color: #5D478B;">,</span> <span style="color: #FF00FF;">&quot;r&quot;</span><span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><span style="color: #00008B; font-style: italic;">//打开的文件不存在</span><br />
&nbsp; &nbsp; &nbsp; err2 <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$ferror</span><span style="color: #9F79EE;">&#40;</span>fd2<span style="color: #5D478B;">,</span> str2<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;File2 descriptor is: %h.&quot;</span><span style="color: #5D478B;">,</span> fd2 <span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span> <span style="color: #00008B; font-style: italic;">//0</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;Error2 number is: %h.&quot;</span><span style="color: #5D478B;">,</span> err2 <span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span> &nbsp; <span style="color: #00008B; font-style: italic;">//非零值</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;Error2 info is: %s.&quot;</span><span style="color: #5D478B;">,</span> str2 <span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span> &nbsp; &nbsp; <span style="color: #00008B; font-style: italic;">//非零值</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$fclose</span><span style="color: #9F79EE;">&#40;</span>fd2<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">end</span><br />
</div></div>
<p><img decoding="async" src="https://www.runoob.com/wp-content/uploads/2021/05/v-file-1.png"></p>
<p>文件打开方式 mode 类型及其描述如下：</p>
<table class="reference"><thead><tr><th style="text-align:left;"><span>r</span></th><th style="text-align:left;"><span>只读打开一个文本文件，只允许读数据。</span></th></tr></thead><tbody><tr><td style="text-align:left;"><span>w</span></td><td style="text-align:left;"><span>只写打开一个文本文件，只允许写数据。如果文件存在，则原文件内容会被删除。如果文件不存在，则创建新文件。</span></td></tr><tr><td style="text-align:left;"><span>a</span></td><td style="text-align:left;"><span>追加打开一个文本文件，并在文件末尾写数据。如果文件如果文件不存在，则创建新文件。</span></td></tr><tr><td style="text-align:left;"><span>rb</span></td><td style="text-align:left;"><span>只读打开一个二进制文件，只允许读数据。</span></td></tr><tr><td style="text-align:left;"><span>wb</span></td><td style="text-align:left;"><span>只写打开或建立一个二进制文件，只允许写数据。</span></td></tr><tr><td style="text-align:left;"><span>ab</span></td><td style="text-align:left;"><span>追加打开一个二进制文件，并在文件末尾写数据。</span></td></tr><tr><td style="text-align:left;"><span>r+</span></td><td style="text-align:left;"><span>读写打开一个文本文件，允许读和写</span></td></tr><tr><td style="text-align:left;"><span>w+</span></td><td style="text-align:left;"><span>读写打开或建立一个文本文件，允许读写。如果文件存在，则原文件内容会被删除。如果文件不存在，则创建新文件。</span></td></tr><tr><td style="text-align:left;"><span>a+</span></td><td style="text-align:left;"><span>读写打开一个文本文件，允许读和写。如果文件不存在，则创建新文件。读取文件会从文件起始地址的开始，写入只能是追加模式。</span></td></tr><tr><td style="text-align:left;"><span>rb+</span></td><td style="text-align:left;"><span>读写打开一个二进制文本文件，功能与 "r+" 类似。</span></td></tr><tr><td style="text-align:left;"><span>wb+</span></td><td style="text-align:left;"><span>读写打开或建立一个二进制文本文件，功能与 "w+" 类似。</span></td></tr><tr><td style="text-align:left;"><span>ab+</span></td><td style="text-align:left;"><span>读写打开一个二进制文本文件，功能与 "a+" 类似。</span></td></tr></tbody></table>
<h3>
文件写入</h3><p>
写文件的系统任务主要包括：$fdisplay, $fwrite, $fstrobe, $fmonitor，以及它们对应的自带格式的系统任务 $fdisplayb, $fdisplayh, $fdisplayo 等。</p>
<table class="reference"><thead><tr><th style="text-align:left;"><span>调用格式</span></th><th style="text-align:left;"><span>任务描述</span></th></tr></thead><tbody><tr><td style="text-align:left;"><span>$fdisplay(fd, arguments) ;</span></td><td style="text-align:left;"><span>按顺序或条件写文件，自动换行</span></td></tr><tr><td style="text-align:left;"><span>$fwrite(fd, arguments) ;</span></td><td style="text-align:left;"><span>按顺序或条件写文件，不自动换行</span></td></tr><tr><td style="text-align:left;"><span>$fstrobe(fd, arguments) ;</span></td><td style="text-align:left;"><span>语句执行完毕后选通写文件</span></td></tr><tr><td style="text-align:left;"><span>$fmonitor(fd, arguments) ;</span></td><td style="text-align:left;"><span>只要数据有变化就写文件</span></td></tr></tbody></table>
<p>相对于标准显示任务 $display, $write, $strobe, $monitor，写文件系统任务除了用法格式上需要多指定文件描述符 fd，其余打印条件、时刻特性等均与其对应的显示任务保持一致。</p>
<p>
利用追加写的方式，对文件进行写操作的举例如下：</p>
<div class="example"><h2 class="example">实例</h2> <div class="example_code">
&nbsp; <span style="color: #00008B; font-style: italic;">//(2) write file</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">integer</span> fd <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">integer</span> err<span style="color: #5D478B;">,</span> str <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">initial</span> <span style="color: #A52A2A; font-weight: bold;">begin</span><br />
&nbsp; &nbsp; &nbsp; fd <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$fopen</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;./DATA_RD.HEX&quot;</span><span style="color: #5D478B;">,</span> <span style="color: #FF00FF;">&quot;a+&quot;</span><span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span> &nbsp;<span style="color: #00008B; font-style: italic;">//末尾追加的方式打开</span><br />
&nbsp; &nbsp; &nbsp; err <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$ferror</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #5D478B;">,</span> str<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #A52A2A; font-weight: bold;">if</span> <span style="color: #9F79EE;">&#40;</span><span style="color: #5D478B;">!</span>err<span style="color: #9F79EE;">&#41;</span> <span style="color: #A52A2A; font-weight: bold;">begin</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #9932CC;">$fdisplay</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #5D478B;">,</span> <span style="color: #FF00FF;">&quot;New data1: %h&quot;</span><span style="color: #5D478B;">,</span> fd<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #9932CC;">$fdisplay</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #5D478B;">,</span> <span style="color: #FF00FF;">&quot;New data2: %h&quot;</span><span style="color: #5D478B;">,</span> str<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #9932CC;">$fdisplay</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #5D478B;">,</span> <span style="color: #FF00FF;">&quot;New data3: %h&quot;</span><span style="color: #5D478B;">,</span> err<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #00008B; font-style: italic;">//$write(fd, &quot;New data3: %h&quot;, err) ; //最后一行不换行打印</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #A52A2A; font-weight: bold;">end</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$fclose</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">end</span><br />
</div></div>
<p>打开文件 DATA_RD.HEX，则可以看到文件末端新增了 3 行数据。</p>
<p><img decoding="async" src="https://www.runoob.com/wp-content/uploads/2021/05/v-file-2.png"></p>
<h3>字符串写入</h3><p>
Verilog 还提供了往字符串里写数据的系统任务 $swrite 和 $sformat。</p>
<table class="reference"><thead><tr><th style="text-align:left;"><span>调用格式</span></th><th style="text-align:left;"><span>任务描述</span></th></tr></thead><tbody><tr><td style="text-align:left;"><span>$swrite(reg, list_of_arguments) ;</span></td><td style="text-align:left;"><span>按顺序或条件写字符串到变量 reg 中</span></td></tr><tr><td style="text-align:left;"><span>len = $sformat(reg, format_str, arguments) ;</span></td><td style="text-align:left;"><span>按格式 format_str 写字符串到变量 reg 中</span><br><span>格式与 $display 指定格式时一致</span><br><span>不建议省略第二个参数 format_str</span><br><span>可返回字符串长度 len</span></td></tr></tbody></table>
<p>$sformat 第二个参数 format 为字符串类型，一般建议不要省略。该参数指定了输入变量的类型，指定类型时也可以包含其他字符串信息，类型种类及用法可参考显示函数 $display。该参数也可以为寄存器类型，但要求存储的数据为正常的字符串数据。</p>
<p>写字符串代码举例如下：</p>
<div class="example"><h2 class="example">实例</h2> <div class="example_code">
&nbsp; &nbsp;<span style="color: #00008B; font-style: italic;">//(3) write string</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">reg</span> <span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">299</span><span style="color: #5D478B;">:</span><span style="color: #ff0055;">0</span><span style="color: #9F79EE;">&#93;</span> str_swrite<span style="color: #5D478B;">,</span> str_sformat<span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">reg</span> <span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">63</span><span style="color: #5D478B;">:</span><span style="color: #ff0055;">0</span><span style="color: #9F79EE;">&#93;</span> str_buf <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">integer</span> &nbsp; &nbsp;len<span style="color: #5D478B;">,</span> age <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">initial</span> <span style="color: #A52A2A; font-weight: bold;">begin</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #5D478B;">#</span><span style="color: #ff0055;">20</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; str_buf &nbsp; <span style="color: #5D478B;">=</span> <span style="color: #FF00FF;">&quot;runoob!&quot;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; age &nbsp; &nbsp; &nbsp; <span style="color: #5D478B;">=</span> <span style="color: #ff0055;">9</span> <span style="color: #5D478B;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #00008B; font-style: italic;">//$swrite 指定格式写包含变量的字符串</span><br />
&nbsp; &nbsp; &nbsp; $swrite<span style="color: #9F79EE;">&#40;</span>str_swrite<span style="color: #5D478B;">,</span> <span style="color: #FF00FF;">&quot;%s age is %d&quot;</span><span style="color: #5D478B;">,</span> str_buf<span style="color: #5D478B;">,</span> age<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;%s&quot;</span><span style="color: #5D478B;">,</span> str_swrite<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #00008B; font-style: italic;">//$swrite 直接写不含有变量的字符串</span><br />
&nbsp; &nbsp; &nbsp; $swrite<span style="color: #9F79EE;">&#40;</span>str_swrite<span style="color: #5D478B;">,</span> <span style="color: #FF00FF;">&quot;years &quot;</span><span style="color: #5D478B;">,</span> <span style="color: #FF00FF;">&quot;old.&quot;</span><span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;%s&quot;</span><span style="color: #5D478B;">,</span> str_swrite<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #00008B; font-style: italic;">//$swrite 不指定格式写包含变量的字符串，不建议</span><br />
&nbsp; &nbsp; &nbsp; $swrite<span style="color: #9F79EE;">&#40;</span>str_swrite<span style="color: #5D478B;">,</span> age<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;$swrite err test: %d&quot;</span><span style="color: #5D478B;">,</span> str_swrite<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #00008B; font-style: italic;">//$sformat 指定格式写包含变量的字符串</span><br />
&nbsp; &nbsp; &nbsp; $sformat<span style="color: #9F79EE;">&#40;</span>str_sformat<span style="color: #5D478B;">,</span> <span style="color: #FF00FF;">&quot;I have learnt in %s&quot;</span><span style="color: #5D478B;">,</span> str_buf<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;%s&quot;</span><span style="color: #5D478B;">,</span> str_sformat<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #00008B; font-style: italic;">//$sformat 直接写不含有变量的字符串，并获取字符串长度</span><br />
&nbsp; &nbsp; &nbsp; len <span style="color: #5D478B;">=</span> $sformat<span style="color: #9F79EE;">&#40;</span>str_sformat<span style="color: #5D478B;">,</span> <span style="color: #FF00FF;">&quot;for 4 years!&quot;</span><span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;%s&quot;</span><span style="color: #5D478B;">,</span> str_sformat<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;$sformat len: %d&quot;</span><span style="color: #5D478B;">,</span> len<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #00008B; font-style: italic;">//$sformat 直接一次写多个不含有变量的字符串，不建议</span><br />
&nbsp; &nbsp; &nbsp; $sformat<span style="color: #9F79EE;">&#40;</span>str_sformat<span style="color: #5D478B;">,</span> <span style="color: #FF00FF;">&quot;for&quot;</span><span style="color: #5D478B;">,</span> <span style="color: #FF00FF;">&quot;4&quot;</span><span style="color: #5D478B;">,</span> <span style="color: #FF00FF;">&quot;years!&quot;</span><span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;$sformat err test: %s&quot;</span><span style="color: #5D478B;">,</span> str_sformat<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">end</span><br />
</div></div><p>
忽略打印信息的空格，调试信息输出如下：</p>
<p><img decoding="async" src="https://www.runoob.com/wp-content/uploads/2021/05/v-file-3.png"></p>
<p>
由此可知，$sformat 与 $swrite 用法可以一致，例如 $sformat 可采用指定格式的写字符串，或只写一次不含变量的字符串。此时 $sformat 相当于在第二个参数中未指定变量类型，所以第三个参数应该忽略不写。</p><p>
$swrite 还可以一次写多个不包含变量的字符串，而 $sformat 不允许如此调用。</p><p>
也建议，使用 $swrite 写包含变量的字符串时要指定变量类型，否则结果可能不可预测。</p>
<h3>文件读取</h3>
<table class="reference"><thead><tr><th style="text-align:left;"><span>系统任务</span></th><th style="text-align:left;"><span>调用格式及说明</span></th><th style="text-align:left;">&nbsp;</th></tr></thead><tbody><tr><td style="text-align:left;"><span>按字符读文件</span></td><td style="text-align:left;"><span>c = $fgetc( fd ) ;</span></td><td style="text-align:left;">&nbsp;</td></tr><tr><td style="text-align:left;">&nbsp;</td><td style="text-align:left;"><span>按字符格式将 fd 数据输出给变量 c，c 位宽最少为 8</span><br><span>读取错误时 c 值为 EOF(-1)，可以用 $ferror 检查错误类型</span></td><td style="text-align:left;">&nbsp;</td></tr><tr><td style="text-align:left;"><span>按字符写缓冲区</span></td><td style="text-align:left;"><span>code = $ungetc(c, fd ) ;</span></td><td style="text-align:left;">&nbsp;</td></tr><tr><td style="text-align:left;">&nbsp;</td><td style="text-align:left;"><span>向文件 fd 缓冲区写字符 c</span><br><span>c 值在下次调用 $fgetc 时返回，文件 fd 自身内容不会发生变化</span><br><span>正常写缓冲时返回值 code 为 0，发生错误时返回值 code 为 EOF</span></td><td style="text-align:left;">&nbsp;</td></tr><tr><td style="text-align:left;"><span>按行读文件</span></td><td style="text-align:left;"><span>code = $fgets(str, fd)</span></td><td style="text-align:left;">&nbsp;</td></tr><tr><td style="text-align:left;">&nbsp;</td><td style="text-align:left;"><span>按字符连续读，直至变量 str 被填满，或一行内容读取完毕，或文件结束</span><br><span>正常读取时返回值 code 为读取行数（次数），发生错误时 code 为 0</span></td><td style="text-align:left;">&nbsp;</td></tr><tr><td style="text-align:left;"><span>按格式读文件</span></td><td style="text-align:left;"><span>code = $fscanf(fd, format, args) ;</span></td><td style="text-align:left;">&nbsp;</td></tr><tr><td style="text-align:left;">&nbsp;</td><td style="text-align:left;"><span>按格式 format 将文件 fd 中的数据读取到变量 args 中</span><br><span>format 可参考 $display 指定格式说明</span><br><span>读取一次的停止条件为空格或换行</span><br><span>读取发生错误时返回值 code 为 0</span></td><td style="text-align:left;">&nbsp;</td></tr><tr><td style="text-align:left;"><span>按格式读字符串</span></td><td style="text-align:left;"><span>code = $sscanf(str, format, args) ;</span></td><td style="text-align:left;">&nbsp;</td></tr><tr><td style="text-align:left;">&nbsp;</td><td style="text-align:left;"><span>按格式 format 将字符串型变量 str 读取到变量 args 中</span><br><span>调用格式方法和 $fscanf 一致</span></td><td style="text-align:left;">&nbsp;</td></tr><tr><td style="text-align:left;"><span>按二进制读文件</span></td><td style="text-align:left;"><span>code = $fread(store, fd, start, count) ;</span></td><td style="text-align:left;">&nbsp;</td></tr><tr><td style="text-align:left;">&nbsp;</td><td style="text-align:left;"><span>按二进制数据流格式将数据从文件 fd 读取到数组或寄存器变量 store 中</span><br><span>start 为文件起始地址，count 为读取长度</span><br><span>若 start/count 未指定，数据会全部填充至变量 store 中</span><br><span>若 store 为寄存器类型，则 start/count 参数无效，store 变量填充满一次数据后便会停止读取</span></td><td style="text-align:left;">&nbsp;</td></tr></tbody></table>
以"文件写入"仿真中的文件 DATA_RD.HEX 为读取的参考文件，进行举例，该文件内容如下。
<pre>c0dec0de
5555aaaa
12345678
aaaa5555
New data1: 80000003
New data2: 00000000
New data3: 00000000</pre>
<p><strong>$fgetc，$ungetc 调用举例</strong></p>
<div class="example"><h2 class="example">实例</h2> <div class="example_code">
&nbsp; &nbsp;<span style="color: #00008B; font-style: italic;">//(4.1) read char</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">integer</span> &nbsp; &nbsp; &nbsp;i <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">reg</span> <span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">31</span><span style="color: #5D478B;">:</span><span style="color: #ff0055;">0</span><span style="color: #9F79EE;">&#93;</span> &nbsp; char_buf <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">initial</span> <span style="color: #A52A2A; font-weight: bold;">begin</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #5D478B;">#</span><span style="color: #ff0055;">30</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; fd <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$fopen</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;DATA_RD.HEX&quot;</span><span style="color: #5D478B;">,</span> <span style="color: #FF00FF;">&quot;r&quot;</span><span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$write</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;Read char: &quot;</span><span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; err <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$ferror</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #5D478B;">,</span> str<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #A52A2A; font-weight: bold;">if</span> <span style="color: #9F79EE;">&#40;</span><span style="color: #5D478B;">!</span>err<span style="color: #9F79EE;">&#41;</span> <span style="color: #A52A2A; font-weight: bold;">begin</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">for</span> <span style="color: #9F79EE;">&#40;</span>i<span style="color: #5D478B;">=</span><span style="color: #ff0055;">0</span><span style="color: #5D478B;">;</span> i<span style="color: #5D478B;">&lt;</span><span style="color: #ff0055;">13</span><span style="color: #5D478B;">;</span> i<span style="color: #5D478B;">++</span><span style="color: #9F79EE;">&#41;</span> <span style="color: #A52A2A; font-weight: bold;">begin</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char_buf<span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">7</span><span style="color: #5D478B;">:</span><span style="color: #ff0055;">0</span><span style="color: #9F79EE;">&#93;</span> <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$fgetc</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span> &nbsp;<span style="color: #00008B; font-style: italic;">//按单个字符读取</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$write</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;%c&quot;</span><span style="color: #5D478B;">,</span> char_buf<span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">7</span><span style="color: #5D478B;">:</span><span style="color: #ff0055;">0</span><span style="color: #9F79EE;">&#93;</span><span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span> <span style="color: #00008B; font-style: italic;">//不换行逐次打印单个字符</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">end</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #9932CC;">$write</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;.<span style="color: #9F79EE">\n</span>&quot;</span><span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #A52A2A; font-weight: bold;">end</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$ungetc</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;1&quot;</span><span style="color: #5D478B;">,</span> fd<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #00008B; font-style: italic;">//连续写3次文件缓冲区</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$ungetc</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;2&quot;</span><span style="color: #5D478B;">,</span> fd<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$ungetc</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;3&quot;</span><span style="color: #5D478B;">,</span> fd<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; char_buf<span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">7</span><span style="color: #5D478B;">:</span><span style="color: #ff0055;">0</span><span style="color: #9F79EE;">&#93;</span> &nbsp; <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$fgetc</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span> &nbsp;<span style="color: #00008B; font-style: italic;">//read 3</span><br />
&nbsp; &nbsp; &nbsp; char_buf<span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">15</span><span style="color: #5D478B;">:</span><span style="color: #ff0055;">8</span><span style="color: #9F79EE;">&#93;</span> &nbsp;<span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$fgetc</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span> &nbsp;<span style="color: #00008B; font-style: italic;">//read 2</span><br />
&nbsp; &nbsp; &nbsp; char_buf<span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">23</span><span style="color: #5D478B;">:</span><span style="color: #ff0055;">16</span><span style="color: #9F79EE;">&#93;</span> <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$fgetc</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span> &nbsp;<span style="color: #00008B; font-style: italic;">//read 1，read buffer end</span><br />
&nbsp; &nbsp; &nbsp; char_buf<span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">31</span><span style="color: #5D478B;">:</span><span style="color: #ff0055;">24</span><span style="color: #9F79EE;">&#93;</span> <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$fgetc</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span> &nbsp;<span style="color: #00008B; font-style: italic;">//read a</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;Read char after $ungetc: %s&quot;</span><span style="color: #5D478B;">,</span> char_buf<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$fclose</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">end</span><br />
</div></div>
<p>仿真结果如下。</p><p>
由图可知，$fgetc 读取的 13 个字符正确，读取字符包括了换行符。</p><p>
$ungetc 向文件缓冲区写字符数据后，再用 $fgetc 可读取文件缓冲区的字符数据。读写遵循先写后出（FILO, First in Last out）原则，相当于压栈。字符数据先写"123"时，读出数据为"321"。</p><p>
文件缓冲区读取完毕后，再进行字符数据读取时，读出的数据依然紧随上一次文件读取的位置，即 log 中"a123"中的字符"a"。</p><p>
此过程中，文件 DATA_RD.HEX 内容一直没有改变。</p>
<p><img decoding="async" src="https://www.runoob.com/wp-content/uploads/2021/05/v-file-4.png"></p>
<p><strong>$fgets 调用举例</strong></p>
<div class="example"><h2 class="example">实例</h2> <div class="example_code">
&nbsp; &nbsp;<span style="color: #00008B; font-style: italic;">//(4.2) read line</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">integer</span> &nbsp; &nbsp; &nbsp;code <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">reg</span> <span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">99</span><span style="color: #5D478B;">:</span><span style="color: #ff0055;">0</span><span style="color: #9F79EE;">&#93;</span> &nbsp; line_buf <span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">9</span><span style="color: #5D478B;">:</span><span style="color: #ff0055;">0</span><span style="color: #9F79EE;">&#93;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">initial</span> <span style="color: #A52A2A; font-weight: bold;">begin</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #5D478B;">#</span><span style="color: #ff0055;">31</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; fd <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$fopen</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;DATA_RD.HEX&quot;</span><span style="color: #5D478B;">,</span> <span style="color: #FF00FF;">&quot;r&quot;</span><span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; err <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$ferror</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #5D478B;">,</span> str<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #A52A2A; font-weight: bold;">if</span> <span style="color: #9F79EE;">&#40;</span><span style="color: #5D478B;">!</span>err<span style="color: #9F79EE;">&#41;</span> <span style="color: #A52A2A; font-weight: bold;">begin</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">for</span> <span style="color: #9F79EE;">&#40;</span>i<span style="color: #5D478B;">=</span><span style="color: #ff0055;">0</span><span style="color: #5D478B;">;</span> i<span style="color: #5D478B;">&lt;</span><span style="color: #ff0055;">6</span><span style="color: #5D478B;">;</span> i<span style="color: #5D478B;">++</span><span style="color: #9F79EE;">&#41;</span> <span style="color: #A52A2A; font-weight: bold;">begin</span> &nbsp;<span style="color: #00008B; font-style: italic;">//按字符串格式逐行读取</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; code <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$fgets</span><span style="color: #9F79EE;">&#40;</span>line_buf<span style="color: #9F79EE;">&#91;</span>i<span style="color: #9F79EE;">&#93;</span><span style="color: #5D478B;">,</span> fd<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span> &nbsp;<span style="color: #00008B; font-style: italic;">//末尾含"\n"，将打印2行</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;Get line data%d: %s&quot;</span><span style="color: #5D478B;">,</span> i<span style="color: #5D478B;">,</span> line_buf<span style="color: #9F79EE;">&#91;</span>i<span style="color: #9F79EE;">&#93;</span><span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">end</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #A52A2A; font-weight: bold;">end</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #00008B; font-style: italic;">//十六进制显示，将显示对应的 ASCIII 码字</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;Show hex line data%d: %h&quot;</span><span style="color: #5D478B;">,</span> <span style="color: #ff0055;">2</span><span style="color: #5D478B;">,</span> line_buf<span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">2</span><span style="color: #9F79EE;">&#93;</span><span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;Show hex line data%d: %h&quot;</span><span style="color: #5D478B;">,</span> <span style="color: #ff0055;">4</span><span style="color: #5D478B;">,</span> line_buf<span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">4</span><span style="color: #9F79EE;">&#93;</span><span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$fclose</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">end</span><br />
</div></div>
<p>仿真结果如下。</p><p>
前 4 行数据按照字符串类型读取和显示，结果正常。</p><p>
读取文件第 5 行数据时，由于变量 line_buf 位宽 100 的限制，文件内容"New data1: 80000003 "需要分 2 次才能完成读取。</p><p>
因为每一行末尾包含换行符"\n"，所以使用 $display 函数打印时，会多出一行空行。</p><p>
按照字符串型读取、并对数据进行十六进制显示时，并不能直观的显示出文件对应的数据内容。例如第二行内容并没有显示"12345678,"而是显示其对应的 ASCII 码。所以 $fgets 任务读取时是按照字符串类型读取的，这里需要注意。</p>
<p><img decoding="async" src="https://www.runoob.com/wp-content/uploads/2021/05/v-file-5.png"></p>

<p><strong>$fscanf，$sscanf 调用举例</strong></p>
<div class="example"><h2 class="example">实例</h2> <div class="example_code">
&nbsp; &nbsp;<span style="color: #00008B; font-style: italic;">//(4.3) $fscanf/$sscanf</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">reg</span> <span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">31</span><span style="color: #5D478B;">:</span><span style="color: #ff0055;">0</span><span style="color: #9F79EE;">&#93;</span> &nbsp; data_buf <span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">9</span><span style="color: #5D478B;">:</span><span style="color: #ff0055;">0</span><span style="color: #9F79EE;">&#93;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">reg</span> <span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">63</span><span style="color: #5D478B;">:</span><span style="color: #ff0055;">0</span><span style="color: #9F79EE;">&#93;</span> &nbsp; string_buf <span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">9</span><span style="color: #5D478B;">:</span><span style="color: #ff0055;">0</span><span style="color: #9F79EE;">&#93;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">reg</span> <span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">31</span><span style="color: #5D478B;">:</span><span style="color: #ff0055;">0</span><span style="color: #9F79EE;">&#93;</span> &nbsp; data_get <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">reg</span> <span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">63</span><span style="color: #5D478B;">:</span><span style="color: #ff0055;">0</span><span style="color: #9F79EE;">&#93;</span> &nbsp; data_test <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">initial</span> <span style="color: #A52A2A; font-weight: bold;">begin</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #5D478B;">#</span><span style="color: #ff0055;">32</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; fd <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$fopen</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;DATA_RD.HEX&quot;</span><span style="color: #5D478B;">,</span> <span style="color: #FF00FF;">&quot;r&quot;</span><span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; err <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$ferror</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #5D478B;">,</span> str<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #A52A2A; font-weight: bold;">if</span> <span style="color: #9F79EE;">&#40;</span><span style="color: #5D478B;">!</span>err<span style="color: #9F79EE;">&#41;</span> <span style="color: #A52A2A; font-weight: bold;">begin</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">for</span> <span style="color: #9F79EE;">&#40;</span>i<span style="color: #5D478B;">=</span><span style="color: #ff0055;">0</span><span style="color: #5D478B;">;</span> i<span style="color: #5D478B;">&lt;</span><span style="color: #ff0055;">4</span><span style="color: #5D478B;">;</span> i<span style="color: #5D478B;">++</span><span style="color: #9F79EE;">&#41;</span> <span style="color: #A52A2A; font-weight: bold;">begin</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #00008B; font-style: italic;">//前4行数据按照十六进制读取和显示</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; code <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$fscanf</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #5D478B;">,</span> <span style="color: #FF00FF;">&quot;%h&quot;</span><span style="color: #5D478B;">,</span> data_buf<span style="color: #9F79EE;">&#91;</span>i<span style="color: #9F79EE;">&#93;</span><span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;$fscanf read data%d: %h&quot;</span><span style="color: #5D478B;">,</span> i<span style="color: #5D478B;">,</span> data_buf<span style="color: #9F79EE;">&#91;</span>i<span style="color: #9F79EE;">&#93;</span><span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">end</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">for</span> <span style="color: #9F79EE;">&#40;</span>i<span style="color: #5D478B;">=</span><span style="color: #ff0055;">4</span><span style="color: #5D478B;">;</span> i<span style="color: #5D478B;">&lt;</span><span style="color: #ff0055;">6</span><span style="color: #5D478B;">;</span> i<span style="color: #5D478B;">++</span><span style="color: #9F79EE;">&#41;</span> <span style="color: #A52A2A; font-weight: bold;">begin</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #00008B; font-style: italic;">//后2行数据按照字符串类型读取和显示</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; code <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$fscanf</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #5D478B;">,</span> <span style="color: #FF00FF;">&quot;%s&quot;</span><span style="color: #5D478B;">,</span> string_buf<span style="color: #9F79EE;">&#91;</span>i<span style="color: #9F79EE;">&#93;</span><span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;$fscanf read data%d: %s&quot;</span><span style="color: #5D478B;">,</span> i<span style="color: #5D478B;">,</span> string_buf<span style="color: #9F79EE;">&#91;</span>i<span style="color: #9F79EE;">&#93;</span><span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">end</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #A52A2A; font-weight: bold;">end</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #00008B; font-style: italic;">//(1) $sscanf 源变量 data_test 为字符串类型</span><br />
&nbsp; &nbsp; &nbsp; data_test <span style="color: #5D478B;">=</span> <span style="color: #FF00FF;">&quot;fedcba98&quot;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; code <span style="color: #5D478B;">=</span> $sscanf<span style="color: #9F79EE;">&#40;</span>data_test<span style="color: #5D478B;">,</span> <span style="color: #FF00FF;">&quot;%h&quot;</span><span style="color: #5D478B;">,</span> data_get<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;$sscanf read data0: %h&quot;</span><span style="color: #5D478B;">,</span> data_get<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #00008B; font-style: italic;">//(2) $sscanf: 将源变量 data_test 先转为字符串变量</span><br />
&nbsp; &nbsp; &nbsp; code <span style="color: #5D478B;">=</span> $sformat<span style="color: #9F79EE;">&#40;</span>data_test<span style="color: #5D478B;">,</span> <span style="color: #FF00FF;">&quot;%h&quot;</span><span style="color: #5D478B;">,</span> data_buf<span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">2</span><span style="color: #9F79EE;">&#93;</span><span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; code <span style="color: #5D478B;">=</span> $sscanf<span style="color: #9F79EE;">&#40;</span>data_test<span style="color: #5D478B;">,</span> <span style="color: #FF00FF;">&quot;%h&quot;</span><span style="color: #5D478B;">,</span> data_get<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #00008B; font-style: italic;">//直接输入十六进制变量是不建议的</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #00008B; font-style: italic;">//code = $sscanf(data_buf[2], &quot;%h&quot;, data_get); </span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;$sscanf read data0: %h&quot;</span><span style="color: #5D478B;">,</span> data_get<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$fclose</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">end</span><br />
</div></div><p>
仿真结果如下。</p><p>
利用 $fscanf 对文件前 4 行内容按照十六进制读取和显示，后 2 行内容按照字符串型读取和显示，均正常。</p><p>
利用 $sscanf 读取源寄存器内容然后搬移到目的寄存器时，源寄存器中的内容应该为字符串型数据。</p><p>

例如，利用 $sscanf 将十六进制的数据 data_buf[2] 搬移到寄存器变量 data_get 时，可以先利用写字符串任务 $sformat 将源变量 data_buf[2] 的内容转为字符串型，存放在变量 data_test 中。然后再利用 $sscanf 按照十六进制将 data_test 中的内容搬移到变量 data_get 中。此时按照十六进制格式打印变量 data_get 会显示正常。</p><p>
如果直接利用 $sscanf 将十六进制格式的数据 data_buf[2] 直接搬移到变量 data_get 中，则 data_get 中的内容将会是异常的。</p><p>
偷偷告诉你，寄存器之间是可以直接赋值的！！！</p>
<p><img decoding="async" src="https://www.runoob.com/wp-content/uploads/2021/05/v-file-6.png"></p>
<p><strong>$fread 调用举例</strong></p>
<div class="example"><h2 class="example">实例</h2> <div class="example_code">
&nbsp; &nbsp;<span style="color: #00008B; font-style: italic;">//(4.4) $fread</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">reg</span> <span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">71</span><span style="color: #5D478B;">:</span><span style="color: #ff0055;">0</span><span style="color: #9F79EE;">&#93;</span> &nbsp; bin_buf <span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">3</span><span style="color: #5D478B;">:</span><span style="color: #ff0055;">0</span><span style="color: #9F79EE;">&#93;</span> <span style="color: #5D478B;">;</span> <span style="color: #00008B; font-style: italic;">//每行有8个字型数据和1个换行符</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">reg</span> <span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">143</span><span style="color: #5D478B;">:</span><span style="color: #ff0055;">0</span><span style="color: #9F79EE;">&#93;</span> &nbsp;bin_reg <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">initial</span> <span style="color: #A52A2A; font-weight: bold;">begin</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #5D478B;">#</span><span style="color: #ff0055;">40</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; fd <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$fopen</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;DATA_RD.HEX&quot;</span><span style="color: #5D478B;">,</span> <span style="color: #FF00FF;">&quot;r&quot;</span><span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; err <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$ferror</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #5D478B;">,</span> str<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #A52A2A; font-weight: bold;">if</span> <span style="color: #9F79EE;">&#40;</span><span style="color: #5D478B;">!</span>err<span style="color: #9F79EE;">&#41;</span> <span style="color: #A52A2A; font-weight: bold;">begin</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;code <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$fread</span><span style="color: #9F79EE;">&#40;</span>bin_buf<span style="color: #5D478B;">,</span> fd<span style="color: #5D478B;">,</span> <span style="color: #ff0055;">0</span><span style="color: #5D478B;">,</span> <span style="color: #ff0055;">4</span><span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span> <span style="color: #00008B; font-style: italic;">//数组型读取，读取4次</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;$fread read data %h&quot;</span><span style="color: #5D478B;">,</span> bin_buf<span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">0</span><span style="color: #9F79EE;">&#93;</span><span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><span style="color: #00008B; font-style: italic;">//十六进制显示</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;$fread read data %h&quot;</span><span style="color: #5D478B;">,</span> bin_buf<span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">1</span><span style="color: #9F79EE;">&#93;</span><span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;$fread read data %s&quot;</span><span style="color: #5D478B;">,</span> bin_buf<span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">2</span><span style="color: #9F79EE;">&#93;</span><span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><span style="color: #00008B; font-style: italic;">//字符串显示</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;$fread read data %s&quot;</span><span style="color: #5D478B;">,</span> bin_buf<span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">3</span><span style="color: #9F79EE;">&#93;</span><span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #A52A2A; font-weight: bold;">end</span><br />
<br />
&nbsp; &nbsp; &nbsp; fd <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$fopen</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;DATA_RD.HEX&quot;</span><span style="color: #5D478B;">,</span> <span style="color: #FF00FF;">&quot;r&quot;</span><span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; code <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$fread</span><span style="color: #9F79EE;">&#40;</span>bin_reg<span style="color: #5D478B;">,</span> fd<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span> <span style="color: #00008B; font-style: italic;">//单个寄存器读取</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;$fread read data %h&quot;</span><span style="color: #5D478B;">,</span> bin_reg<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$fclose</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">end</span><br />
</div></div>
<p>仿真结果如下。</p><p>
$fread 按二进制读取文件时 ，起始地址和读取长度都是设置数组型变量的参数。</p><p>
如果存储数据的变量类型是非数组的 reg 型，则只会进行一次读取，直至 reg 型变量被填充完毕。</p>
<p><img decoding="async" src="https://www.runoob.com/wp-content/uploads/2021/05/v-file-7.png"></p>
<h3>
文件定位</h3>
<table class="reference"><thead><tr><th style="text-align:left;"><span>系统任务</span></th><th style="text-align:left;"><span>调用格式</span></th><th style="text-align:left;"><span>任务描述</span></th></tr></thead><tbody><tr><td style="text-align:left;"><span>获取文件位置</span></td><td style="text-align:left;"><span>pos = $ftell( fd ) ;</span></td><td style="text-align:left;"><span>返回文件当前位置距离文件首部的偏移量，初始地址为 0</span><br><span>偏移量按照字节为一单位（8bits）</span><br><span>配合 $fseek 使用</span></td></tr><tr><td style="text-align:left;"><span>重定位</span></td><td style="text-align:left;"><span>code = $fseek(fd, offset, type) ;</span></td><td style="text-align:left;"><span>设置文件下一个输入或输出的位置</span><br><span>offset 为设置的偏移量</span><br><span>type 为偏移量的操作类型</span><br><span>--- 0: 设置位置到偏移地址</span><br><span>--- 1: 设置位置到当前位置加偏移量</span><br><span>--- 2: 设置位置到文件尾加偏移量，经常使用负数来表示文件尾向前的偏移量</span></td></tr><tr><td style="text-align:left;"><span>无偏移重定位</span></td><td style="text-align:left;"><span>code = $rewind( fd ) ;</span></td><td style="text-align:left;"><span>等价于 $fseek( fd, 0, 0) ;</span></td></tr><tr><td style="text-align:left;"><span>判断文件尾部</span></td><td style="text-align:left;"><span>code = $feof(fd) ;</span></td><td style="text-align:left;"><span>判读是否到文件尾部</span><br><span>检测到文件尾部时返回值为 1，否则为 0</span></td></tr></tbody></table><p>
文件 DATA_RD.HEX 内容可表示如下。</p><p>

换行符"\n"为结束符，则文件大小为：4x9 + 3x20 = 96 byte。</p>
<p><img decoding="async" src="https://www.runoob.com/wp-content/uploads/2021/05/5DFBDFE8-9113-4188-BF53-7CC3656B6CE3.jpg"></p>
<p>文件定位测试代码如下：</p>
<div class="example"><h2 class="example">实例</h2> <div class="example_code">
&nbsp; &nbsp;<span style="color: #00008B; font-style: italic;">// file position</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">reg</span> <span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">31</span><span style="color: #5D478B;">:</span><span style="color: #ff0055;">0</span><span style="color: #9F79EE;">&#93;</span> &nbsp; data4 <span style="color: #5D478B;">;</span> &nbsp; &nbsp; &nbsp;<span style="color: #00008B; font-style: italic;">//寄存器变量长度为 4bytes</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">reg</span> <span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">199</span><span style="color: #5D478B;">:</span><span style="color: #ff0055;">0</span><span style="color: #9F79EE;">&#93;</span> &nbsp;str_long <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">integer</span> &nbsp; &nbsp; &nbsp;pos <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">initial</span> <span style="color: #A52A2A; font-weight: bold;">begin</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #5D478B;">#</span><span style="color: #ff0055;">40</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; fd <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$fopen</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;DATA_RD.HEX&quot;</span><span style="color: #5D478B;">,</span> <span style="color: #FF00FF;">&quot;r&quot;</span><span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; err <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$ferror</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #5D478B;">,</span> str<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #A52A2A; font-weight: bold;">if</span> <span style="color: #9F79EE;">&#40;</span><span style="color: #5D478B;">!</span>err<span style="color: #9F79EE;">&#41;</span> <span style="color: #A52A2A; font-weight: bold;">begin</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #00008B; font-style: italic;">//first read</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;code <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$fscanf</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #5D478B;">,</span> <span style="color: #FF00FF;">&quot;%h&quot;</span><span style="color: #5D478B;">,</span> data4<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><span style="color: #00008B; font-style: italic;">//从0位置开始读</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pos &nbsp;<span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$ftell</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span> &nbsp; &nbsp; &nbsp;<span style="color: #00008B; font-style: italic;">//读8byte后位置为8,坐标为(0,8)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;Position after read: %d&quot;</span><span style="color: #5D478B;">,</span> pos<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;1st read data: %h&quot;</span><span style="color: #5D478B;">,</span> data4<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #00008B; font-style: italic;">//type = 0</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;code <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$fseek</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #5D478B;">,</span> <span style="color: #ff0055;">4</span><span style="color: #5D478B;">,</span> <span style="color: #ff0055;">0</span><span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span> <span style="color: #00008B; font-style: italic;">//从位置4、坐标(0,4)开始读</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;code <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$fscanf</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #5D478B;">,</span> <span style="color: #FF00FF;">&quot;%h&quot;</span><span style="color: #5D478B;">,</span> data4<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span> <span style="color: #00008B; font-style: italic;">//读到换行符停止</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pos &nbsp;<span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$ftell</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span> &nbsp; &nbsp; &nbsp;<span style="color: #00008B; font-style: italic;">//读4byte后位置为8,坐标为(0,8)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;type 0: current position: %d&quot;</span><span style="color: #5D478B;">,</span> pos<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;type 0: read data: %h&quot;</span><span style="color: #5D478B;">,</span> data4<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #00008B; font-style: italic;">//type = 1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;code <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$fseek</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #5D478B;">,</span> <span style="color: #ff0055;">4</span><span style="color: #5D478B;">,</span> <span style="color: #ff0055;">1</span><span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span> <span style="color: #00008B; font-style: italic;">//从位置4+9=12、坐标(1,3)据开始读</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;code <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$fscanf</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #5D478B;">,</span> <span style="color: #FF00FF;">&quot;%h&quot;</span><span style="color: #5D478B;">,</span> data4<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span> <span style="color: #00008B; font-style: italic;">//读到换行符停止</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pos &nbsp;<span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$ftell</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span> &nbsp; &nbsp; &nbsp;<span style="color: #00008B; font-style: italic;">//读5byte后位置为17，坐标为(1,8)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;type 1: current position: %d&quot;</span><span style="color: #5D478B;">,</span> pos<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;type 1: read data: %h&quot;</span><span style="color: #5D478B;">,</span> data4<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #00008B; font-style: italic;">//type = 2</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;code <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$fseek</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #5D478B;">,</span> <span style="color: #5D478B;">-</span><span style="color: #9F79EE;">&#40;</span><span style="color: #ff0055;">96</span><span style="color: #5D478B;">-</span><span style="color: #ff0055;">31</span><span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">,</span> <span style="color: #ff0055;">2</span><span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span> <span style="color: #00008B; font-style: italic;">//从位置31、坐标(3,4)开始读</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;code <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$fscanf</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #5D478B;">,</span> <span style="color: #FF00FF;">&quot;%h&quot;</span><span style="color: #5D478B;">,</span> data4<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pos &nbsp;<span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$ftell</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span> &nbsp; &nbsp; &nbsp;<span style="color: #00008B; font-style: italic;">//读4byte后位置为35，坐标为(3,8)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;type 2: current position: %d&quot;</span><span style="color: #5D478B;">,</span> pos<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;type 2: read data: %h&quot;</span><span style="color: #5D478B;">,</span> data4<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #00008B; font-style: italic;">//rewind read</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;code &nbsp; <span style="color: #5D478B;">=</span> $rewind<span style="color: #9F79EE;">&#40;</span>fd<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><span style="color: #00008B; font-style: italic;">//重新将文件指针的位置指向文件首部</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pos &nbsp; &nbsp;<span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$ftell</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span> &nbsp;<span style="color: #00008B; font-style: italic;">//此时位置为 0</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;Position after $rewind: %d&quot;</span><span style="color: #5D478B;">,</span> pos<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #00008B; font-style: italic;">//read all content of file</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">while</span> <span style="color: #9F79EE;">&#40;</span><span style="color: #5D478B;">!</span><span style="color: #9932CC;">$feof</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #9F79EE;">&#41;</span><span style="color: #9F79EE;">&#41;</span> <span style="color: #A52A2A; font-weight: bold;">begin</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; code &nbsp; <span style="color: #5D478B;">=</span> <span style="color: #9932CC;">$fgets</span><span style="color: #9F79EE;">&#40;</span>str_long<span style="color: #5D478B;">,</span> fd<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$write</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;Read : %s&quot;</span><span style="color: #5D478B;">,</span> str_long<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">end</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #9932CC;">$fclose</span><span style="color: #9F79EE;">&#40;</span>fd<span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #A52A2A; font-weight: bold;">end</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">end</span><br />
</div></div><p>
仿真结果如下。</p><p>
由图可知 log 末尾多打了一行数据，这是因为文件 DATA_RD.TXT 末尾还有一行空白行（换行操作之后的结果），系统任务 $feof 并不认为该空白行为文件尾部，所以返回值仍然为 0。但实际该行并没有数据，所以读取的数据具有不可控制性。</p><p>
为消除文件最后一行数据中换行符的影响，可将"文件写入"例子中最后一个写文件系统任务 $fdisplay 替换为 $write 。</p><p>
其余 log 结合代码注释可知仿真正确，这里不再做统一解释。</p>
<p><img decoding="async" src="https://www.runoob.com/wp-content/uploads/2021/05/v-file-8.png"></p>
<h3>加载存储器</h3>
<table class="reference"><thead><tr><th style="text-align:left;"><span>系统任务</span></th><th style="text-align:left;"><span>调用格式及说明</span></th><th style="text-align:left;">&nbsp;</th></tr></thead><tbody><tr><td style="text-align:left;"><span>加载十六进制文件</span></td><td style="text-align:left;"><span>$readmemh("fname", mem, start_addr, finish_addr)</span></td><td style="text-align:left;">&nbsp;</td></tr><tr><td style="text-align:left;">&nbsp;</td><td style="text-align:left;"><span>fname 为数据文件名字</span><br><span>mem 为数组型/存储器型变量</span><br><span>start_addr、finish_addr 分别为起始地址和终止地址</span><br><span>start_addr、finish_addr 可以省略，此时加载数据的停止条件为存储器变量 mem 被填充完毕，或文件读取完毕</span><br><span>文件内容只应该有空白符（或换行、空格符）、二进制或十六进制数据</span><br><span>注释用"//"进行标注，数据间建议用换行符区分</span></td><td style="text-align:left;">&nbsp;</td></tr><tr><td style="text-align:left;"><span>加载二进制文件</span></td><td style="text-align:left;"><span>$readmemb("fname", mem, start_addr, finish_addr)</span></td><td style="text-align:left;">&nbsp;</td></tr><tr><td style="text-align:left;">&nbsp;</td><td style="text-align:left;"><span>用法格式同 $readmemb</span></td><td style="text-align:left;">&nbsp;</td></tr></tbody></table>
<p>
文件 DATA_WITHNOTE.HEX 内容如下，将此文件的内容加载到存储器变量中。</p>
<p><img decoding="async" src="https://www.runoob.com/wp-content/uploads/2021/05/v-file-9.png"></p>
<p>举例代码如下：</p>
<div class="example"><h2 class="example">实例</h2> <div class="example_code">
&nbsp; &nbsp;<span style="color: #00008B; font-style: italic;">//6 load mem</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">reg</span> <span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">31</span><span style="color: #5D478B;">:</span><span style="color: #ff0055;">0</span><span style="color: #9F79EE;">&#93;</span> &nbsp; mem_load <span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">3</span><span style="color: #5D478B;">:</span><span style="color: #ff0055;">0</span><span style="color: #9F79EE;">&#93;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">initial</span> <span style="color: #A52A2A; font-weight: bold;">begin</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #5D478B;">#</span><span style="color: #ff0055;">50</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$readmemh</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;./DATA_WITHNOTE.HEX&quot;</span><span style="color: #5D478B;">,</span> mem_load<span style="color: #9F79EE;">&#41;</span><span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;Read memory1: %h&quot;</span><span style="color: #5D478B;">,</span> mem_load<span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">0</span><span style="color: #9F79EE;">&#93;</span><span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;Read memory2: %h&quot;</span><span style="color: #5D478B;">,</span> mem_load<span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">1</span><span style="color: #9F79EE;">&#93;</span><span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;Read memory3: %h&quot;</span><span style="color: #5D478B;">,</span> mem_load<span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">2</span><span style="color: #9F79EE;">&#93;</span><span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #9932CC;">$display</span><span style="color: #9F79EE;">&#40;</span><span style="color: #FF00FF;">&quot;Read memory4: %h&quot;</span><span style="color: #5D478B;">,</span> mem_load<span style="color: #9F79EE;">&#91;</span><span style="color: #ff0055;">3</span><span style="color: #9F79EE;">&#93;</span><span style="color: #9F79EE;">&#41;</span> <span style="color: #5D478B;">;</span><br />
&nbsp; &nbsp;<span style="color: #A52A2A; font-weight: bold;">end</span><br />
</div></div>
<p>仿真结果如下：</p>
<p><img decoding="async" src="https://www.runoob.com/wp-content/uploads/2021/05/v-file-10.png"></p>
<h3>本章节源码下载</h3><p>
<a class="download" href="../wp-content/uploads/2021/05/v7.2_fifo_io.zip">Download</a>   </p>				</div>
			</div>
			<div class="previous-next-links">
			<div class="previous-design-link">← <a href="../w3cnote/verilog2-display.html" rel="prev"> 7.1 Verilog 显示任务</a> </div>
			<div class="next-design-link"><a href="../w3cnote/verilog2-random.html" rel="next"> 7.3 Verilog 随机数及概率分布</a> →</div>
			</div>
						<div class="article-heading-ad" id="w3cnote-ad728">
			<script async src=""></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="horizontal"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
			</div>
			<style>
@media screen and (max-width: 768px) {
	#w3cnote-ad728 {
		display: none;
	}
}
p.note-author {
    border-bottom: 1px solid #ddd;
    font-size: 18px;
    font-weight: bold;
    color: #78a15a;
    padding-bottom: 2px;
    margin-bottom: 4px;
}
</style>
<script>
var aid = 23813;
</script>
	</div>
		
	</div>
	<div class="listcol last right-column">

	<script type="text/javascript">
jQuery(document).ready(function ($){
	var total = $(".membership li").length;
	var left_list = '';
	href = window.location.href;
	
	$(".membership li").each(function(index, value){

		left_list += $(this).html();

		cur_href = $(this).find("a").attr("href");
		cur_obj = $(this);
		
		if(href.match(cur_href) != null) {
			if(index==0) {
				$(".previous-design-link").hide();
			}
			if(index==(total-1)) {
				$(".next-design-link").hide();
			}
			prev_href = cur_obj.prev("li").find("a").attr("href");
			prev_title = cur_obj.prev("li").find("a").attr("title");

			next_href = cur_obj.next("li").find("a").attr("href");
			next_title = cur_obj.next("li").find("a").attr("title");
			if(prev_title) {
				$(".previous-design-link a").attr("href", prev_href);
				$(".previous-design-link a").attr("title", prev_title);
				$(".previous-design-link a").text(prev_title);
			} else {
				$(".previous-design-link").html("");
			}

			if(next_title) {
				$(".next-design-link a").attr("href", next_href);
				$(".next-design-link a").attr("title", next_title);
				$(".next-design-link a").text(next_title);
			} else {
				$(".next-design-link").html("");
			}
		}
	});

	$("#leftcolumn").html(left_list);
});
	</script>
	<div class="sidebar-box cate-list">
	<div class="sidebar-box recommend-here list-link">
	<a href="javascript:void(0);">Verilog 高级教程</a>	</div>
	
	<ul class="membership">
		
	<li><a target="_top" data-id="23639" title="0.1 数字逻辑设计" href="../w3cnote/verilog2-tutorial.html" >0.1 数字逻辑设计</a></li>
	
		
	<li><a target="_top" data-id="23641" title="0.2 Verilog 编码风格" href="../w3cnote/verilog2-codestyle.html" >0.2 Verilog 编码风格</a></li>
	
		
	<li><a target="_top" data-id="23644" title="0.3 Verilog 代码规范" href="../w3cnote/verilog2-codeguide.html" >0.3 Verilog 代码规范</a></li>
	
		
	<li><a target="_top" data-id="23645" title="1.1 Verilog 门的类型" href="../w3cnote/verilog2-gate.html" >1.1 Verilog 门的类型</a></li>
	
		
	<li><a target="_top" data-id="23648" title="1.2 Verilog 开关级建模" href="../w3cnote/verilog2-level-modeling.html" >1.2 Verilog 开关级建模</a></li>
	
		
	<li><a target="_top" data-id="23656" title="1.3 Verilog 门延迟" href="../w3cnote/verilog2-gate-delay.html" >1.3 Verilog 门延迟</a></li>
	
		
	<li><a target="_top" data-id="23673" title="2.1 Verilog UDP 基础知识" href="../w3cnote/verilog2-udp.html" >2.1 Verilog UDP 基础知识</a></li>
	
		
	<li><a target="_top" data-id="23674" title="2.2 Verilog 组合逻辑 UDP" href="../w3cnote/verilog2-udp-logic.html" >2.2 Verilog 组合逻辑 UDP</a></li>
	
		
	<li><a target="_top" data-id="23677" title="2.3 Verilog 时序逻辑 UDP" href="../w3cnote/verilog2-udp-timing.html" >2.3 Verilog 时序逻辑 UDP</a></li>
	
		
	<li><a target="_top" data-id="23685" title="3.1 Verilog 延迟模型" href="../w3cnote/verilog2-delay-type.html" >3.1 Verilog 延迟模型</a></li>
	
		
	<li><a target="_top" data-id="23690" title="3.2 Verilog specify 块语句" href="../w3cnote/verilog2-specify.html" >3.2 Verilog specify 块语句</a></li>
	
		
	<li><a target="_top" data-id="23692" title="3.3 Verilog 建立时间和保持时间" href="../w3cnote/verilog2-setup-hold-time.html" >3.3 Verilog 建立时间和保持时间</a></li>
	
		
	<li><a target="_top" data-id="23701" title="3.4 Verilog 时序检查" href="../w3cnote/verilog2-timing-check.html" >3.4 Verilog 时序检查</a></li>
	
		
	<li><a target="_top" data-id="23708" title="3.5 Verilog 延迟反标注" href="../w3cnote/verilog2-sdf.html" >3.5 Verilog 延迟反标注</a></li>
	
		
	<li><a target="_top" data-id="23714" title="4.1 Verilog 同步与异步" href="../w3cnote/verilog-sync.html" >4.1 Verilog 同步与异步</a></li>
	
		
	<li><a target="_top" data-id="23725" title="4.2 Verilog 跨时钟域传输：慢到快" href="../w3cnote/verilog2-slow2fast.html" >4.2 Verilog 跨时钟域传输：慢到快</a></li>
	
		
	<li><a target="_top" data-id="23729" title="4.3 Verilog 跨时钟域传输：快到慢" href="../w3cnote/verilog2-fast2slow.html" >4.3 Verilog 跨时钟域传输：快到慢</a></li>
	
		
	<li><a target="_top" data-id="23731" title="4.4 Verilog FIFO 设计" href="../w3cnote/verilog2-fifo.html" >4.4 Verilog FIFO 设计</a></li>
	
		
	<li><a target="_top" data-id="23739" title="5.1 Verilog 复位简介" href="../w3cnote/verilog2-reset.html" >5.1 Verilog 复位简介</a></li>
	
		
	<li><a target="_top" data-id="23743" title="5.2 Verilog 时钟简介" href="../w3cnote/verilog2-clock.html" >5.2 Verilog 时钟简介</a></li>
	
		
	<li><a target="_top" data-id="23757" title="5.3 Verilog 时钟分频" href="../w3cnote/verilog2-clock-division.html" >5.3 Verilog 时钟分频</a></li>
	
		
	<li><a target="_top" data-id="23768" title="5.4 Verilog 时钟切换" href="../w3cnote/verilog2-clock-switch.html" >5.4 Verilog 时钟切换</a></li>
	
		
	<li><a target="_top" data-id="23779" title="6.1 Verilog 低功耗简介" href="../w3cnote/verilog2-low-power.html" >6.1 Verilog 低功耗简介</a></li>
	
		
	<li><a target="_top" data-id="23788" title="6.2 Verilog 系统级低功耗设计" href="../w3cnote/verilog2-lower-power-design.html" >6.2 Verilog 系统级低功耗设计</a></li>
	
		
	<li><a target="_top" data-id="23792" title="6.3 Verilog  RTL 级低功耗设计（上）" href="../w3cnote/verilog2-rtl-low-power-design-1.html" >6.3 Verilog  RTL 级低功耗设计（上）</a></li>
	
		
	<li><a target="_top" data-id="23796" title="6.4 Verilog RTL 级低功耗设计（下）" href="../w3cnote/verilog2-rtl-low-power-design-2.html" >6.4 Verilog RTL 级低功耗设计（下）</a></li>
	
		
	<li><a target="_top" data-id="23806" title="7.1 Verilog 显示任务" href="../w3cnote/verilog2-display.html" >7.1 Verilog 显示任务</a></li>
	
		<li>
	7.2 Verilog 文件操作	</li>
	
		
	<li><a target="_top" data-id="23825" title="7.3 Verilog 随机数及概率分布" href="../w3cnote/verilog2-random.html" >7.3 Verilog 随机数及概率分布</a></li>
	
		
	<li><a target="_top" data-id="23847" title="7.4 Verilog 实数整数转换" href="../w3cnote/verilog2-real2int.html" >7.4 Verilog 实数整数转换</a></li>
	
		
	<li><a target="_top" data-id="23851" title="7.5 Verilog 其他系统任务" href="../w3cnote/verilog2-other-task.html" >7.5 Verilog 其他系统任务</a></li>
	
		
	<li><a target="_top" data-id="23862" title="8.1 Verilog  PLI 简介" href="../w3cnote/verilog2-pli-intro.html" >8.1 Verilog  PLI 简介</a></li>
	
		
	<li><a target="_top" data-id="23865" title="8.2 Verilog TF 子程序" href="../w3cnote/verilog2-tf.html" >8.2 Verilog TF 子程序</a></li>
	
		
	<li><a target="_top" data-id="23869" title="8.3 Verilog TF 子程序列表" href="../w3cnote/verilog2-tf-sub.html" >8.3 Verilog TF 子程序列表</a></li>
	
		
	<li><a target="_top" data-id="23870" title="8.4 Verilog ACC 子程序" href="../w3cnote/verilog2-acc.html" >8.4 Verilog ACC 子程序</a></li>
	
		
	<li><a target="_top" data-id="23872" title="8.5 Verilog ACC 子程序列表" href="../w3cnote/verilog2-acc-sub.html" >8.5 Verilog ACC 子程序列表</a></li>
	
		
	<li><a target="_top" data-id="23876" title="9.1 Verilog 逻辑综合" href="../w3cnote/verilog2-logic-sumarry.html" >9.1 Verilog 逻辑综合</a></li>
	
		
	<li><a target="_top" data-id="23882" title="9.2 Verilog 可综合性设计" href="../w3cnote/verilog2-integrated-design.html" >9.2 Verilog 可综合性设计</a></li>
	
	</ul></div>	</div>
</div>


<!-- 底部 -->
<div id="footer" class="mar-t50">
   <div class="runoob-block">
    <div class="runoob cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a target="_blank" href="../html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a target="_blank" href="../jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a target="_blank" href="../charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="../tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a target="_blank" href="https://c.runoob.com/front-end/6939/">JS 混淆/加密</a>
      </dd> 
      <dd>
       &middot; <a target="_blank" href="https://c.runoob.com/front-end/6232/">PNG/JPEG 图片压缩</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="../tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="..//c.runoob.com/front-end/53">JSON 格式化工具</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="..//c.runoob.com/front-end/6680/">随机数生成器</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="../matplotlib/matplotlib-imread.html" title="Matplotlib imread() 方法">Matplotlib imre...</a>
      </dd>
              <dd>
       &middot;
      <a href="../matplotlib/matplotlib-imsave.html" title="Matplotlib imsave() 方法">Matplotlib imsa...</a>
      </dd>
              <dd>
       &middot;
      <a href="../matplotlib/matplotlib-imshow.html" title="Matplotlib imshow() 方法">Matplotlib imsh...</a>
      </dd>
              <dd>
       &middot;
      <a href="../matplotlib/matplotlib-hist.html" title="Matplotlib 直方图">Matplotlib 直方图</a>
      </dd>
              <dd>
       &middot;
      <a href="../python3/python-func-object.html" title="Python object() 函数">Python object()...</a>
      </dd>
              <dd>
       &middot;
      <a href="../python3/python-ai-draw.html" title="Python AI 绘画">Python AI 绘画</a>
      </dd>
              <dd>
       &middot;
      <a href="../w3cnote/cursor-editor.html" title="神辅助 Cursor 编辑器，加入 GPT-4 让编码更轻松！">神辅助 Cursor ...</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="mailto:admin@runoob.com" rel="external nofollow">意见反馈</a>
       </dd>
      <dd>
       &middot;
      <a target="_blank" href="../disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a target="_blank" href="../aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a target="_blank" href="../archives">文章归档</a>
      </dd>
    
     </dl>
    
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="128" height="128" src="/wp-content/themes/runoob/assets/images/qrcode.png" />
       </div>
     </div>
     
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2023    <strong><a href="../" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="../" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：<a target="_blank" rel="nofollow" href="https://beian.miit.gov.cn/">闽ICP备15012807号-1</a>
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" style="display:none" href="javascript:void(0)"   title="标记/收藏"><i class="fa fa-star" aria-hidden="true"></i></a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="/wp-content/themes/runoob/assets/images/qrcode.png"></div> 
    </div>
  </div>

 <div style="display:none;">
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-84264393-2"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-84264393-2');
</script>
<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?3eec0b7da6548cf07db3bc477ea905ee";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>

</div>
<script>
window.jsui={
    www: 'https://www.runoob.com',
    uri: 'https://www.runoob.com/wp-content/themes/runoob'
};
</script>

<script src=""></script>
<script src=""></script>

</body>
</html>